package cn.snmp.mutual.uk.event;

import java.util.*;

import cn.snmp.mutual.uk.stack.Pdu;

public class RequestPduReceivedSupport {
    public static final String version_id = "@(#)$Id: RequestPduReceivedSupport.java,v 1.4 2006/01/17 17:59:33 birgit Exp $ Copyright Westhawk Ltd";
    private Object source;
    private transient Vector pduListeners;

    public RequestPduReceivedSupport(final Object src) {
        this.source = src;
    }

    public synchronized void empty() {
        if (this.pduListeners != null) {
            this.pduListeners.removeAllElements();
        }
    }

    public synchronized int getListenerCount() {
        int c = 0;
        if (this.pduListeners != null) {
            c = this.pduListeners.size();
        }
        return c;
    }

    public synchronized void addRequestPduListener(final RequestPduListener listener) {
        if (this.pduListeners == null) {
            this.pduListeners = new Vector(5);
        }
        if (!this.pduListeners.contains(listener)) {
            this.pduListeners.addElement(listener);
        }
    }

    public synchronized void removeRequestPduListener(final RequestPduListener listener) {
        if (this.pduListeners != null) {
            this.pduListeners.removeElement(listener);
        }
    }

    public void fireRequestPduReceived(final Pdu pdu, final int hostPort) {
        Vector copyOfListeners = null;
        if (this.pduListeners != null) {
            synchronized (this.pduListeners) {
                copyOfListeners = (Vector) this.pduListeners.clone();
            }
        }
        if (copyOfListeners != null) {
            final int sz = copyOfListeners.size();
            for (int i = sz - 1; i >= 0; --i) {
                final RequestPduListener listener = (RequestPduListener) copyOfListeners.elementAt(i);
                final RequestPduEvent evt = new RequestPduEvent(this.source, pdu, hostPort);
                listener.requestPduReceived(evt);
            }
        }
    }
}
